package com.test.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Leet1115 {
	public static void main(String[] args) {
		FooBar fooBar = new FooBar();

		new Thread(new Runnable() {
			@Override
			public void run() {
				fooBar.foo();
			}
		}).start();

		new Thread(new Runnable() {
			@Override
			public void run() {
				fooBar.bar();
			}
		}).start();
	}
}

class FooBar {
	private int n;
	Lock lock=new ReentrantLock();
	Condition c1=lock.newCondition();
	Condition c2=lock.newCondition();

	public void foo() {
		lock.lock();
		try{
			for (int i = 0; i < 3; i++) {

				// printFoo.run() outputs "foo". Do not change or remove this line.
				System.out.print(111);
				c2.signal();
				c1.await();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally{
			lock.unlock();
		}
	}

	public void bar() {
		lock.lock();
		try{
			for (int i = 0; i < 3; i++) {

				c2.await();
				// printBar.run() outputs "bar". Do not change or remove this line.
				System.out.print(222);
				c1.signal();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally{
			lock.unlock();
		}
	}
}


